package com.example.demo.service.impl;

import com.example.demo.model.SiteSettings;
import com.example.demo.repository.SiteSettingsRepository;
import com.example.demo.service.SiteSettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class SiteSettingsServiceImpl implements SiteSettingsService {
    
    private final SiteSettingsRepository siteSettingsRepository;
    
    @Autowired
    public SiteSettingsServiceImpl(SiteSettingsRepository siteSettingsRepository) {
        this.siteSettingsRepository = siteSettingsRepository;
    }
    
    @Override
    @Transactional(readOnly = true)
    public List<SiteSettings> getAllSettings() {
        return siteSettingsRepository.findAll();
    }
    
    @Override
    @Transactional(readOnly = true)
    public Optional<SiteSettings> getSettingByKey(String key) {
        return siteSettingsRepository.findBySettingKey(key);
    }
    
    @Override
    @Transactional(readOnly = true)
    public String getSettingValue(String key, String defaultValue) {
        Optional<SiteSettings> setting = siteSettingsRepository.findBySettingKey(key);
        return setting.map(SiteSettings::getSettingValue).orElse(defaultValue);
    }
    
    @Override
    @Transactional
    public SiteSettings saveSetting(String key, String value, String description) {
        Optional<SiteSettings> existingSetting = siteSettingsRepository.findBySettingKey(key);
        
        if (existingSetting.isPresent()) {
            SiteSettings setting = existingSetting.get();
            setting.setSettingValue(value);
            if (description != null) {
                setting.setDescription(description);
            }
            return siteSettingsRepository.save(setting);
        } else {
            SiteSettings newSetting = new SiteSettings(key, value, description);
            return siteSettingsRepository.save(newSetting);
        }
    }
    
    @Override
    @Transactional
    public SiteSettings saveSetting(SiteSettings setting) {
        if (setting.getSettingKey() == null || setting.getSettingKey().trim().isEmpty()) {
            throw new IllegalArgumentException("设置键不能为空");
        }
        
        Optional<SiteSettings> existingSetting = 
                siteSettingsRepository.findBySettingKey(setting.getSettingKey());
        
        if (existingSetting.isPresent() && !existingSetting.get().getId().equals(setting.getId())) {
            throw new IllegalArgumentException("设置键已存在: " + setting.getSettingKey());
        }
        
        return siteSettingsRepository.save(setting);
    }
    
    @Override
    @Transactional
    public void deleteSetting(Long id) {
        siteSettingsRepository.deleteById(id);
    }
    
    @Override
    @Transactional
    public void deleteSettingByKey(String key) {
        Optional<SiteSettings> setting = siteSettingsRepository.findBySettingKey(key);
        setting.ifPresent(siteSettingsRepository::delete);
    }
} 